<%--
  Created by IntelliJ IDEA.
  User: jacksonmelius
  Date: 11/26/12
  Time: 1:27 PM
  To change this template use File | Settings | File Templates.
--%>

<%@ page contentType="text/html;charset=UTF-8" %>
<html>
<head>
  <title></title>
    <meta name="layout" content="main">
    <g:javascript library="jquery" plugin="jquery"/>
    <link rel="stylesheet" type="text/css" href="<g:resource dir='css' file='style.css' />" />

    <r:require module="jquery"/>
    <r:script>
        $().ready(
                function() {
                    $("#searchForm").on("submit", function() {
                        var id = $('input:radio[name=searchTypes]:checked').val()
                        //alert(id)
                        if(id == "User") {
                            var type = "searchUser"
                        }
                        else if(id == "Game") {
                            var type = "searchGame"
                        }
                        else if(id == "Platform") {
                            var type = "searchPlatform"
                        }
                        var query = $("input[name='query']").val();
                        //if(query != "" & query != " ") {
                            var blah = $.ajax({
                                url: "/FinalProject/search/index",
                                data: { query: query, type: type }
                            });
                            blah.done(function(data){
                                $("#searchResults").replaceWith(data);
                            });
                            return false;
                        //}
                    });
                })
    </r:script>

</head>


<body>

<h1>
    Jackson + Danny's Videogame Emporium
</h1>

<div class="backdrop">

<div class="tab"><g:link action="showAllUsers" controller="SiteUser" elementId="tabText">USERS</g:link></div>
<div class="tab"><g:link action="showAllGames" controller="SiteUser" elementId="tabText">GAMES</g:link></div>
<div class="tab"><g:link action="showAllPlatforms" controller="SiteUser" elementId="tabText">PLATFORMS</g:link></div>

<div class="mainSection">
<div id="textSection">
<g:if test = "${viewType == "SiteUser"}">
<div>
    <span id="title">${user.name}</span> <br>

    <br>
Reviews by this user:


<ul>
   <g:each in="${user.reviews}">
       <br> <li class="review"> <g:link action="showGame" controller="SiteUser" id = "${it.game.id}">${it.game.name}</g:link> <br> ${it.reviewText} <br> --Posted ${it.dateCreated}</li>
   </g:each>
</ul>

</div>

</g:if>

<g:if test = "${viewType == "Game"}">
    <span id="title">${game.name}</span> <br>

    Platform: <g:link action = "showPlatform" controller="SiteUser" id = "${game.platform.id}">${game.platform.name}</g:link>  <br>
    <br>
    Reviews for ${game.name}:

    <ul>
        <g:each in="${game.reviews}">
            <br> <li class="review">${it.reviewText} <br>--Posted ${it.dateCreated} by <g:link action="show" controller="SiteUser" id = "${it.siteUser.id}">${it.siteUser.name}</g:link></li>
        </g:each>
    </ul>
    <br>
    <div id = "update">
        Add a review for ${game.name}:
        <g:form action="makeNew" method="GET">
           Review: <g:textArea name = "update" rows="8" cols="50"/> <br>
           Username: <g:textField name = "userName">USERNAME</g:textField>
            <g:hiddenField name="id" value = "${game.id}">ID</g:hiddenField>
            <g:hiddenField name="type" value = "Game">TYPE</g:hiddenField>
            <input type="submit" value="Submit Review">
        </g:form>
    </div>

</g:if>

<g:if test = "${viewType == "Platform"}">

    <span id="title">${platform.name}</span>  <br>

    Games for this platform:

    <ul>
        <g:each in="${platform.games}">
            <br> <li><g:link action = "showGame" controller="SiteUser" id = "${it.id}">${it.name}</g:link> (released ${it.releaseDate})</li>
        </g:each>
    </ul>
    <br>
    <div id = "update">
        Add a game for the ${platform.name}:

        <g:form action="makeNew" method="GET">
            Game Name: <g:textField name = "gameName">GAMENAME</g:textField>  <br>
            Release Date: <g:textField name = "releaseDate">RELEASEDATE</g:textField>
            <g:hiddenField name="id" value = "${platform.id}">ID</g:hiddenField>
            <g:hiddenField name="type" value = "Platform"></g:hiddenField>
            <input type="submit" value="Add Game">
        </g:form>
    </div>
</g:if>
</div>



<div id="searchSection">


    <g:form id="searchForm" name="searchForm">
        <g:textField name="query"/>
        <input type="submit" value="Search" />
    </g:form>

<g:radioGroup name="searchTypes"
              labels="['Search Users:','Search Games:','Search Platforms:']"
              values="['User','Game','Platform']"
              value='User'>
    <p>${it.label} ${it.radio}</p>
</g:radioGroup>

<%--
<g:select name="searchTypes"
          from="${['Search Users','Search Games', 'Search Platforms']}"
          keys="${['User','Game','Platform']}"
    />
--%>

    <div id = "searchResults"></div>

</div>
<div id="clear"></div>
</div>
</div>

</body>
</html>